﻿$(function () {
    $.ajax({
        type: 'GET',
        url: '/Ashx/Acc/Staff/IndexAccordion.ashx?url=' + window.location.pathname + '&rnd=' + Math.random(),
        data: 'id=0',
        success: function (items) {
            // $.messager.alert('item:', items, 'info');
            var item = jQuery.parseJSON(items);
            if (item.length > 0) {
                for (var i = 0; i < item.length; i++) {
                    $('#Accordion').accordion('add', {
                        title: item[i].text,
                        content: '<ul class="easyui-tree" data-options="url:\'/Ashx/Acc/Staff/IndexAccordion.ashx?id=' + item[i].id + '&url=' + window.location.pathname + '&rnd=' + Math.random() + '\',method:\'get\',animate:true,lines:true,onClick: function(node){node.url != null && node.url != \'\' ? window.xbase.openit(node.text, node.url) : void(0); }"></ul>',
                    });
                }
                $('#Accordion').accordion('select', 0); //选择第一个
            }
        },
    });
    $('#tt').tabs({
        onLoad: function (panel) {
            var plugin = panel.panel('options').title;
            panel.find('textarea[name="code-' + plugin + '"]').each(function () {
                var data = $(this).val();
                data = data.replace(/(\r\n|\r|\n)/g, '\n');
                if (data.indexOf('\t') == 0) {
                    data = data.replace(/^\t/, '');
                    data = data.replace(/\n\t/g, '\n');
                }
                data = data.replace(/\t/g, '    ');
                var pre = $('<pre name="code" class="prettyprint linenums"></pre>').insertAfter(this);
                pre.text(data);
                $(this).remove();
            });
            prettyPrint();
        }
    });
    //绑定tabs的右键菜单
    $("#tt").tabs({
        onContextMenu: function (e, title) {
            e.preventDefault();
            $('#tabsMenu').menu('show', {
                left: e.pageX,
                top: e.pageY
            }).data("tabTitle", title);
        }
    });
    //实例化menu的onClick事件
    $("#tabsMenu").menu({
        onClick: function (item) {
            CloseTab(this, item.name);
        }
    });
    //几个关闭事件的实现
    function CloseTab(menu, type) {
        var curTabTitle = $(menu).data("tabTitle");
        var tabs = $("#tt");

        if (type === "close") { tabs.tabs("close", curTabTitle); return; }

        var allTabs = tabs.tabs("tt");
        var closeTabsTitle = [];

        $.each(allTabs, function () {
            var opt = $(this).panel("options");
            if (opt.closable && opt.title != curTabTitle && type === "Other") {
                closeTabsTitle.push(opt.title);
            } else if (opt.closable && type === "All") {
                closeTabsTitle.push(opt.title);
            }
        });

        for (var i = 0; i < closeTabsTitle.length; i++) {
            tabs.tabs("close", closeTabsTitle[i]);
        }
    }
    $(window).resize(function () {
        $('#main-layout').layout('resize');
    });
});
